package com.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 路由模式:绑定交换机
 * @author IT-GYS
 *
 */
@Configuration
public class DirectMQConfig 
{
	@Value("${rabbitmq.queue.direct1}")
	String queueName1;
	@Value("${rabbitmq.queue.direct2}")
	String queueName2;

	@Value("${rabbitmq.exchange.direct}")
	String exchangeKey;

	@Value("${rabbitmq.routing.direct1}")
	String routingKey1;
	@Value("${rabbitmq.routing.direct2}")
	String routingKey2;

	@Bean(value = "${rabbitmq.queue.direct1}")
	public Queue initOrderCancelQueue1(){
	    return new Queue(queueName1) ;
	}
	
	@Bean(value = "${rabbitmq.queue.direct2}")
	public Queue initOrderCancelQueue2(){
	    return new Queue(queueName2) ;
	}
	
	@Bean(value = "${rabbitmq.exchange.direct}")
	public DirectExchange topicExchange(){
	    return new DirectExchange (exchangeKey);
	}

	@Bean
	public Binding bindingA(@Qualifier(value = "${rabbitmq.queue.direct1}") Queue queue1,
	                       @Qualifier(value = "${rabbitmq.exchange.direct}") DirectExchange exchange){
	    return BindingBuilder
	            .bind(queue1)
	            .to(exchange)
	            .with(routingKey1);
	}
	
	@Bean
	public Binding bindingB(@Qualifier(value = "${rabbitmq.queue.direct2}") Queue queue2,
	                       @Qualifier(value = "${rabbitmq.exchange.direct}") DirectExchange exchange){
	    return BindingBuilder
	            .bind(queue2)
	            .to(exchange)
	            .with(routingKey1);
	}
	
	@Bean
	public Binding bindingC(@Qualifier(value = "${rabbitmq.queue.direct1}") Queue queue1,
	                       @Qualifier(value = "${rabbitmq.exchange.direct}") DirectExchange exchange){
	    return BindingBuilder
	            .bind(queue1)
	            .to(exchange)
	            .with(routingKey2);
	}
}
